package com.bw.common.utils

import android.os.CountDownTimer
import android.widget.TextView

fun Long.secondsToMillis() = this * 1000L
class CountdownExample (
    private val totalTimeSeconds:Long = 60L,
    private val countDownIntervalSeconds:Long = 1L,
    private val text:String = "倒计时结束!"
){
    fun startCountdown(textView: TextView){
        val countDownTimer = object :CountDownTimer(
            totalTimeSeconds.secondsToMillis(),
            countDownIntervalSeconds.secondsToMillis()
        ){
            override fun onTick(millisUntilFinished: Long) {
                val secondsRemaining = millisUntilFinished/1000
                textView.text = formatTime(secondsRemaining)
                textView.isEnabled = false
            }

            override fun onFinish() {
                textView.text = text
                textView.isEnabled = true
            }
        }
        countDownTimer.start()
    }

    private fun formatTime(seconds:Long):String{
        val minutes = seconds/60
        val remainingSeconds = seconds%60
        return "${seconds}秒"
    }
}